use "$out/HS0temp1_bnoSext_2022Sept.dta", clear

merge 1:1 anon t using "$in/admin3_alap.dta", keep(master match) nogen keepusing(vallazon1 ev w1 wh1 kor ferfi fogvisz1)

* construct EMP (employment) and NONEMP (non-employment) variables *
gen     EMP = 1 if (vallazon1!=.) & ((w1!=. & w1>0) | (wh1!=. & wh1>0)) & (sick_leave_days<6 | sick_leave_days==.) & (fogvisz1==201)
	
codebook anon	
	
drop if HS==1

codebook anon

* flag one observation randomly within each anon *
	set seed 54321
	generate random = uniform()
	by anon (random), sort: gen flag = _n == 1
* gen m for the flagged time period, within each anon
gen m = 0 if flag==1

gen tm = t if flag==1
preserve
	keep if flag==1
	keep anon tm
	rename tm mintm
	sort anon
	tempfile tm
	save `tm'
restore
sort anon
merge m:1 anon using `tm'
drop if _m==2
drop _merge

replace m = t-mintm
drop mintm

gen korm = kor if flag==1
preserve
	keep if flag==1
	keep anon korm
	rename korm minkorm
	sort anon
	tempfile korm
	save `korm'
restore
sort anon
merge m:1 anon using `korm'
drop if _m==2
drop _merge

keep if minkorm>=20 & minkorm<=50
drop minkorm

codebook anon

drop if (m<-24 | m>24) & m!=.

* keep if the year corresponding to m=0 is at least 2009 *
gen yearm = ev if m==0
preserve
	keep if m==0
	keep anon yearm
	rename yearm mintyearm
	sort anon
	tempfile yearm
	save `yearm'
restore
sort anon
merge m:1 anon using `yearm'
drop if _m==2
drop _merge
drop if mintyearm<2009
drop yearm mintyearm

codebook anon

sort anon t

* get the first month of EMP after the shock
egen firstEMPtemp = min(m) if m>0 & EMP==1, by(anon)	
egen firstEMP = max(firstEMPtemp), by(anon)
drop firstEMPtemp

egen EMPm0 = mean(EMP) if m==0, by(anon) 
* keep all anons (and all their observations) for whom either Wml1==1 | Wm0==1
foreach var of varlist EMPm0 {
	egen max`var' = max(`var'), by(anon)
	}

keep if (maxEMPm0 == 1)


keep if m==0
keep anon t ev firstEMP

label var firstEMP "first month of EMP=1 after m=0"

sum
save "$out/potential_controls_updated_tlm_bnoSext2_SL5_nod_2024Apr22.dta"
